var server = "smartgym.ocmcom.com";
if (wx.getStorageSync("mydebug")) server = "smartgym.ocmcom.com";
const host = `https://${server}/smallapp/Member`;
var MD5 = require("md5.js");
function requestTread(obj) {
  var timestamp = Date.parse(new Date()) / 1000;
  var key = "ocmcom_me";
  var url = `https://${server}/smallapp/Treadmill` + obj.url
  obj.data.time = timestamp;
  obj.data.sign = MD5.hexMD5(key + "_" + timestamp);
  obj.data.shop_id = obj.data.shop_id ? obj.data.shop_id : wx.getStorageSync("shop_id");
  console.log("net requestTread", obj);
  wx.request({
    url: url,
    data: obj.data,
    method: "POST",
    success: function (res) { console.log("net success:", res); if (obj.success) obj.success(res); },
    fail: function (res) { console.log("net fail:", res); if (obj.fail) obj.fail(res); },
    complete: function (res) { if (obj.complete) obj.complete(res); },
  })

}
function request(obj){
  var timestamp = Date.parse(new Date())/1000;
  var key = "ocmcom_me";
  var url = host + obj.url
  obj.data.time = timestamp;
  obj.data.sign = MD5.hexMD5(key + "_" + timestamp);
  obj.data.shop_id = obj.data.shop_id ? obj.data.shop_id:wx.getStorageSync("shop_id");
  console.log("net request",obj);
  wx.request({
    url: url,
    data: obj.data,
    method:"POST",
    success: function (res) {console.log("net success:",res); if (obj.success) obj.success(res);},
    fail: function (res) { console.log("net fail:", res); if (obj.fail) obj.fail(res);},
    complete: function (res) { if (obj.complete) obj.complete(res);},
  })
}
function uploadFile(obj){
  var timestamp = Date.parse(new Date()) / 1000;
  var key = "ocmcom_me";
  var url = host + obj.url
  obj.formData.time = timestamp;
  obj.formData.sign = MD5.hexMD5(key + "_" + timestamp);
  console.log("net uploadFile", obj);
  wx.uploadFile({
    url: url, //仅为示例，非真实的接口地址
    filePath: obj.filePath,
    name: obj.name,
    formData: obj.formData,
    success: function (res) {
      console.log("net success:", res)
      if(obj.success)obj.success(res);
    },fail(res){
      console.log("net fail:", res)
      if (obj.fail) obj.fail(res);
    }, complete() {
      if (obj.complete) obj.complete();
    }
  })
}

module.exports={
  request: request,
  requestTread: requestTread,
  uploadFile: uploadFile
}